import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;

public class Main extends JFrame
{
	private JButton  btn_BinarySearch,
					 btn_Matrix,
					 btn_BubbleSort,
					 btn_StackQueue;
	
	private ClosableTabbedPane av_TabFrame;
	
	// these counters are printed in the tab title when new tabs are created
	private int bubblesort_tab_count,
				binarytree_tab_count,
				matrix_tab_count,
				stack_tab_count;
	
	private JMenuItem exitItem, aboutItem;

	public static void main(String[] args)
	{
		
	    try {
		    // Set cross-platform Java L&F (also called "Metal")
	        UIManager.setLookAndFeel(
	            UIManager.getSystemLookAndFeelClassName());
	    } 
	    catch (UnsupportedLookAndFeelException e) {
	       // handle exception
	    } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		Main window = new Main();
		
	}

	public Main()
	{
		
		// initialize tab counters to zero
		bubblesort_tab_count = 1;
		binarytree_tab_count = 1;
		matrix_tab_count = 1;
		stack_tab_count = 1;
		
		this.setSize(new Dimension(1100,600));
		this.setTitle("AlgoVision");
		
		// inside our Main window container, add a new panel that will hold the main layout
		// which is the menu bar, buttons, and tabbed frames
		JPanel MainFrame = new JPanel();
		MainFrame.setLayout(new BorderLayout());
		MainFrame.add(TopPanel(), BorderLayout.NORTH);
		MainFrame.setVisible(true);

		av_TabFrame = new ClosableTabbedPane();
		MainFrame.add(av_TabFrame, BorderLayout.CENTER);
		
		av_TabFrame.add("Welcome  ", welcomePanel());
	
		this.setContentPane(MainFrame);	
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	
	}
	
	// contains the menu bar and the button bar, maybe separate into two?
	private JPanel TopPanel()
	{	

		// create the buttons to allow the user to change between functionalities
		btn_BinarySearch = new JButton();
		btn_Matrix = new JButton();
		btn_StackQueue = new JButton();
		btn_BubbleSort = new JButton();
		
		btn_BinarySearch.setText("Binary Trees");
		btn_Matrix.setText("Matrices");
		btn_StackQueue.setText("Stacks and Queues");
		btn_BubbleSort.setText("Bubble Sort");
		
		//add the button listeners
		btn_BinarySearch.addActionListener(new initialButtonListener());
		btn_Matrix.addActionListener(new initialButtonListener());
		btn_StackQueue.addActionListener(new initialButtonListener());
		btn_BubbleSort.addActionListener(new initialButtonListener());
		
		// add the buttons to their own panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(btn_StackQueue);
		buttonPanel.add(btn_BubbleSort);
		buttonPanel.add(btn_Matrix);
		buttonPanel.add(btn_BinarySearch);
		
		// the menu bar File, Help, etc.
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		exitItem = new JMenuItem("Exit");
		aboutItem = new JMenuItem("About");
		menuBar.add(fileMenu);
		fileMenu.add(aboutItem);
		fileMenu.add(exitItem);

		exitItem.addActionListener(new initialButtonListener());
		aboutItem.addActionListener(new initialButtonListener());
		
		// add the button panel and the menu bar to one panel
		JPanel menuPanel = new JPanel();
		menuPanel.setLayout(new BorderLayout());
		menuPanel.add(menuBar, BorderLayout.NORTH);
		menuPanel.add(buttonPanel, BorderLayout.CENTER);

		return menuPanel;
	}
	
	private JPanel welcomePanel()
	{
		
		JPanel thisPanel = new JPanel();
		thisPanel.setBackground(Color.white);
		
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(new File("avlogo.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
		picLabel.setAlignmentX(CENTER_ALIGNMENT);	
		thisPanel.add(Box.createRigidArea(new Dimension(0,160)));
		thisPanel.add( picLabel );	
		return thisPanel;
		
		
	}

	private JFrame aboutFrame()
	{
		JFrame thisFrame = new JFrame();
		thisFrame.setSize( new Dimension(300,200));
		
		JPanel aboutPanel = new JPanel();
		aboutPanel.setPreferredSize( new Dimension(300,200));

		aboutPanel.setLayout(new BoxLayout(aboutPanel, BoxLayout.Y_AXIS));
		
		Font LabelFont = new Font("Dialog", 1, 14);
		
		aboutPanel.add(Box.createRigidArea(new Dimension(0,10)));
		
		JLabel avLabel = new JLabel("AlgoVision");
		avLabel.setFont(LabelFont);
		avLabel.setAlignmentX(CENTER_ALIGNMENT);		
		aboutPanel.add(avLabel);
		
		JLabel copyLabel = new JLabel("Copyright 2011");
		copyLabel.setFont(LabelFont);
		copyLabel.setAlignmentX(CENTER_ALIGNMENT);
		aboutPanel.add(copyLabel);
		
		aboutPanel.add(Box.createRigidArea(new Dimension(0,10)));
		
		JLabel jtLabel = new JLabel("Jonathon Turel");
		jtLabel.setFont(LabelFont);
		jtLabel.setAlignmentX(CENTER_ALIGNMENT);
		aboutPanel.add(jtLabel);
		
		JLabel prLabel = new JLabel("Pamela Rosoff");
		prLabel.setFont(LabelFont);
		prLabel.setAlignmentX(CENTER_ALIGNMENT);
		aboutPanel.add(prLabel);
		
		JLabel jcLabel = new JLabel("Justin Cowen");
		jcLabel.setFont(LabelFont);
		jcLabel.setAlignmentX(CENTER_ALIGNMENT);
		aboutPanel.add(jcLabel);
		
		JLabel asLabel = new JLabel("Ashford Smith");
		asLabel.setFont(LabelFont);
		asLabel.setAlignmentX(CENTER_ALIGNMENT);
		aboutPanel.add(asLabel);
		
		thisFrame.add(aboutPanel);
		
		return thisFrame;
		
	}
	
	private class initialButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			if (e.getSource() == btn_BinarySearch) 
			{
				av_TabFrame.add("BinaryTree" + binarytree_tab_count + "  ", new BinaryTreePanel());
				binarytree_tab_count++;
			}
			else if (e.getSource() == btn_Matrix)
			{
				av_TabFrame.add("Matrix" + matrix_tab_count + "  ", new MatrixPanel());
				matrix_tab_count++;
			}
			else if (e.getSource() == btn_StackQueue)
			{
				av_TabFrame.add("StackQueue" + stack_tab_count + "  ", new StackQueuePanel());
				stack_tab_count++;
			}
			else if (e.getSource() == btn_BubbleSort)
			{
				av_TabFrame.add("BubbleSort" + bubblesort_tab_count + "  ", new BubbleSortPanel());
				bubblesort_tab_count++;
			}
			else if (e.getSource() == exitItem)
			{
				System.exit(0);
			}
			else if (e.getSource() == aboutItem)
			{

				JFrame frame = aboutFrame();				
				frame.setVisible(true);
				
			}

		}	
	}
}

